package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.domain.StudentClass;
import com.bbu.javaexam.service.StudentClassService;
import com.bbu.javaexam.util.RespDataHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName StudentClassController.java
 * @Description TODO
 * @createTime 2022年03月06日 19:16:00
 */
@RestController
@RequestMapping("/class")
@Slf4j
public class StudentClassController {

    @Autowired
    private StudentClassService studentClassService;

    @GetMapping("/{page}/{size}/{teacherid}")
    public RespData list(@PathVariable("teacherid") Integer teacherid, @PathVariable("page") Integer page, @PathVariable("size") Integer pageSize) {
        Page<StudentClass> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<StudentClass> lam = new LambdaQueryWrapper<>();
        lam.eq(StudentClass::getTeacherid, teacherid);
        studentClassService.page(page1, lam);
        return RespDataHandler.success(page1);
    }

    @PostMapping("/add")
    public RespData add(@RequestBody StudentClass studentClass) {
        log.info(studentClass.toString());
        boolean save = studentClassService.save(studentClass);
        if (save) {
            return RespDataHandler.success(studentClass);
        }
        return RespDataHandler.buildRespData(100, "添加失败", null);
    }

    @PutMapping
    public RespData update(@RequestBody StudentClass studentClass) {
        log.info(studentClass.toString());
        boolean save = studentClassService.updateById(studentClass);
        if (save) {
            return RespDataHandler.success(studentClass);
        }
        return RespDataHandler.buildRespData(100, "添加失败", null);
    }

    @GetMapping("/{id}")
    public RespData getOne(@PathVariable("id") Long id) {
        StudentClass studentClass = studentClassService.getById(id);
        return RespDataHandler.success(studentClass);
    }

    @DeleteMapping("/{id}")
    public RespData delete(@PathVariable("id") Long id) {
        boolean remove = studentClassService.removeById(id);
        return RespDataHandler.success(remove);
    }

}
